package com.zuimei.zuimei.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Html;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by Jogger on 2017/1/22.
 * text文本内图片
 */

public class NetworkImageGetter implements Html.ImageGetter {

    private Context mContext;

    public NetworkImageGetter(Context context) {
        mContext = context;
    }

    @Override
    public Drawable getDrawable(String source) {//source为文本内容内的图片链接
        InputStream is = null;
        try {
            HttpURLConnection connection = (HttpURLConnection) new URL(source).openConnection();
            if (connection.getResponseCode()==200){
                is=connection.getInputStream();
                Drawable d = Drawable.createFromStream(is, "src");
                int width = ScreenUtil.getSrceenWidth(mContext) - 60;
                int height = (int) (width / ((float) d.getIntrinsicWidth() / d.getIntrinsicHeight
                        ()));
                d.setBounds(30, 0, width, height);
                return d;
            }
            //is = (InputStream) new URL(source).getContent();
            //等比例设置图片宽高

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}
